<template>
  <div @click="goBack" class="reelect-box">
    <div class="reelect-txt">返回</div>
  </div>
  <canvas ref="canvas" class="video-player"></canvas>
</template>
<script setup lang="ts">
import { ref, onMounted, onUnmounted } from "vue";
import { useRouter } from "vue-router";

const classRef = ref<HTMLElement>();
const canvas = ref<HTMLCanvasElement>();
const router = useRouter();

const video = ref<HTMLVideoElement>(document.createElement("video"));
video.value.src = new URL(
  "@/assets/webm/panda_classroom_animation.webm",
  import.meta.url
).href;
video.value.addEventListener("loadedmetadata", () => {
  if (canvas.value) {
    canvas.value.width = canvas.value.offsetWidth;
    canvas.value.height = canvas.value.offsetHeight;
  }
});
video.value.autoplay = true;
video.value.loop = true;
video.value.muted = true;
video.value.playsInline = true;

let animationFrameId: number;

// 修改绘制逻辑
function drawCanvas() {
  if (canvas.value && video.value) {
    const ctx = canvas.value.getContext("2d", { alpha: true }); // 启用alpha通道
    if (ctx) {
      // 初始化时填充透明背景
      ctx.fillStyle = "rgba(0,0,0,0)";
      ctx.fillRect(0, 0, canvas.value.width, canvas.value.height);
      const videoRatio = video.value.videoWidth / video.value.videoHeight;
      ctx.clearRect(0, 0, canvas.value.width, canvas.value.height);
      let drawWidth = canvas.value.width;
      let drawHeight = canvas.value.height;
      let offsetX = 0;
      let offsetY = 0;
      const canvasRatio = canvas.value.width / canvas.value.height;
      if (canvasRatio > videoRatio) {
        drawWidth = canvas.value.width;
        drawHeight = canvas.value.width / videoRatio;
        offsetY = (canvas.value.height - drawHeight) / 2;
      } else {
        drawHeight = canvas.value.height;
        drawWidth = canvas.value.height * videoRatio;
        offsetX = (canvas.value.width - drawWidth) / 2;
      }
      ctx.drawImage(video.value, offsetX, offsetY, drawWidth, drawHeight);
    }
  }
  animationFrameId = requestAnimationFrame(drawCanvas);
}

function goBack() {
  router.back();
}

onMounted(() => {
  const classRefDom = classRef.value;
  if (classRefDom) {
    classRefDom.classList.toggle("background-2");
  }
  video.value.play().then(() => {
    drawCanvas();
  });
});

onUnmounted(() => {
  cancelAnimationFrame(animationFrameId);
  video.value.pause();
});
</script>
<style lang="scss" scoped>
.reelect-box {
  position: absolute;
  margin: calc(10px + var(--dynamic-top)) 0 0 20px;
  width: 64px;
  height: 28px;
  z-index: 5;
  background: url("@/assets/image/back.png");
  background-repeat: no-repeat;
  background-size: 100% 100%;
  .reelect-txt {
    font-family: Source Han Sans;
    font-size: 14px;
    font-weight: 500;
    line-height: 28px;
    letter-spacing: 0px;
    font-variation-settings: "opsz" auto;
    color: #ffffff;
    margin: 0 0 0 20px;
    text-align: center;
  }
}
.class-room-vessel {
  width: 100vw;
  height: 100vh;
}
.video-container {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  background: rgba(0, 0, 0, 0);
}
// 更新canvas样式
.video-player {
  position: absolute;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background-color: rgba(0, 0, 0, 0) !important; // 使用RGBA透明
  -webkit-background-clip: padding-box; // iOS兼容属性
  -webkit-transform: translateZ(0); // 强制硬件加速
  transform: translateZ(0);
  border: none;
  pointer-events: none;
}
.desktop {
  position: absolute;
  right: 0;
  top: 100px;
  width: 365px;
  height: 530px;
  z-index: 99;
}
</style>